<?php

class AdminController extends Controller
{
	public $layout = 'awt';
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionSearchTranslation()
	{
		$songSearcher = new SongSearcher();
		
		Yii::import('ext.zsearcher.Zsearcher');
		$searcher = new Zsearcher($songSearcher);
		$searcher->normalSearch();

		$this->render('search',array(
			'result'	=>$searcher->getResult(),
			'pager'		=>$searcher->getPager(),
			'filter'	=>$searcher->getFilter(),
		));
	}
	
	public function actionChooseSongTranslation()
	{
		$songSearcher = new SongLinkSearcher();
		
		Yii::import('ext.zsearcher.Zsearcher');
		$searcher = new Zsearcher($songSearcher);
		$searcher->normalSearch();

		$this->render('search',array(
			'result'	=>$searcher->getResult(),
			'pager'		=>$searcher->getPager(),
			'filter'	=>$searcher->getFilter(),
		));
	}
	
	public function actionChooseOriginal($id) {
		$rs1 = Song::model()->with('album')->findByPk($id);
		$rs2 = array('id'=>$rs1->id,'name'=>$rs1->name,'album'=>$rs1->album->name);
		Fish::sSet('chooseOriginal',$rs2);
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	public function actionChooseTranslator($id) {
		$rs1 = Song::model()->with('album')->findByPk($id);
		$rs2 = array('id'=>$rs1->id,'name'=>$rs1->name,'album'=>$rs1->album->name);
		Fish::sAdd('chooseTranslator',$rs2,$rs2['id']);
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	public function actionChooseSame($id) {
		$rs1 = Song::model()->with('album')->findByPk($id);
		$rs2 = array('id'=>$rs1->id,'name'=>$rs1->name,'album'=>$rs1->album->name);
		Fish::sAdd('chooseSame',$rs2,$rs2['id']);
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	
	public function actionClearOriginal(){
		Fish::sDel('chooseOriginal');
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	public function actionClearAll(){
		Fish::sDel('chooseOriginal');
		Fish::sDel('chooseTranslator');
		Fish::sDel('chooseSame');
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	public function actionClearTranslator($id) {
		Fish::sDel('chooseTranslator',$id);
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	public function actionClearSame($id) {
		Fish::sDel('chooseSame',$id);
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	public function actionEditLink($id) {
		
		Fish::sDel('chooseOriginal');
		Fish::sDel('chooseTranslator');
		Fish::sDel('chooseSame');
		
		# find second
		$rs2 = Yii::app()->db->createCommand()
			->select('*')
			->from('song_link')
			->where('song_id2=:song_id2',array(':song_id2'=>$id))
			->queryAll();
		
		if ($rs2!==false && isset($rs2[0])) {
			$id = $rs2[0]['song_id'];
		}
		# find original
		$rs1 = Yii::app()->db->createCommand()
			->select('a.id,a.song_id as oid,a.song_id2 as tid,a.link_type_id,b.name,b.album_id,c.name as album_name')
			->from('song_link a')
			->join('song b','a.song_id2=b.id')
			->join('album c','b.album_id=c.id')
			->where('song_id=:song_id',array(':song_id'=>$id))
			->queryAll();
		if ($rs1===false) {
			throw new CHttpException(404,'Translation or Link Not Found');
		}
		
		# set original
		$rs3 = Song::model()->with('album')->findByPk($id);
		$rs2 = array('id'=>$id,'name'=>$rs3->name,'album'=>$rs3->album->name);
		Fish::sSet('chooseOriginal',$rs2);
		
		foreach ($rs1 as $row) {

			if ($row['link_type_id']=='1') {
				
				$rs4 = array('id'=>$row['tid'],'name'=>$row['name'],'album'=>$row['album_name']);
				Fish::sAdd('chooseSame',$rs4,$rs4['id']);
				
			} else {

				$rs4 = array('id'=>$row['tid'],'name'=>$row['name'],'album'=>$row['album_name']);
				Fish::sAdd('chooseTranslator',$rs4,$rs4['id']);
			}

		}
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}

	
	public function actionCreateTranslation() {
		#need confirmation to post again?
		#find the oid, tid
		#if found any of them, delete all and insert new
	}
	
	public function actionQuickviewTranslation($id) {
		
		$this->layout = 'awt-quickview';
		
		$rs = Song::model()->with('lyric')->findByPk($id);
		
		$this->render('quickview',array(
			'rs'	=>$rs
		));
	}
	
	public function actionUpdateTranslation() {
		
		$model = new Song();
		
		if ( !empty($_POST) && isset($_POST['Song']) ) {
			
			$model->attributes = $_POST['Song'];
			if ($model->save()) {
				
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
			$this->refresh();
		}
		
		$this->render('create',array(
			'model'=>$model,
		));
	}

	public function actionDeleteTranslation() {
		
		if ( !empty($_POST) && isset($_POST['Song']) ) {
			$rs = Song::model()->deleteByPk($_POST['Song']['id']);
			if ($rs) {
				Yii::app()->user->setFlash('success',"Song Deleted");
			} else {
				echo "<pre>"; print_r( $rs->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
		}
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	
	# must have translation first before create lyric
	public function actionCreateLyric() {
		$songSearcher = new SongSearcher();
		
		Yii::import('ext.zsearcher.Zsearcher');
		$searcher = new Zsearcher($songSearcher);
		$searcher->normalSearch();

		$this->render('create',array(
			'result'	=>$searcher->getResult(),
			'pager'		=>$searcher->getPager(),
			'filter'	=>$searcher->getFilter(),
		));
	}

	public function actionUpdateLyric($id) {

		$lyrics = Yii::app()->db->createCommand()
			->select('*')
			->from('lyric a')
			->where('a.song_id=:id', array(':id'=>$id))
			->order('line_no asc')
			->queryAll();

		if ( !empty($_POST) && isset($_POST['lyric']) ) {

			Yii::app()->db->createCommand()
				->delete('lyric','song_id=:id',array(':id'=>$id));

			foreach ($_POST['lyric'] as $key => $lyric) {
				if (!empty($lyric)) {
					Yii::app()->db->createCommand()
						->insert('lyric',array(
							'song_id' =>$id,
							'line_no' =>$key+1,
							'name'    =>$_POST['name'][$key],
							'content' =>$lyric,
							'pinyin'  =>'',
						));
				}
			}
			$this->refresh();
		}
		$this->render('update',array(
			'lyric'=>$lyrics,
		));
	}
}